<%-- 
    Document   : administracaoProcessamentoPlanilha
    Created on : 25/10/2010, 08:06:01
    Author     : Thiago
--%>

<%@page import="processamento.ReadExcel"%>
<%@page language="java" import="org.apache.commons.fileupload.*, java.util.*, java.io.*" %>
<%@page import="java.net.URLEncoder"%>
<%@page import="crypt.Crypt"%>
<%@page import="mail.EmailSender"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="bo.*"%>
<%@page import="beans.*"%>
<%@page import="autenticacao.Sessao"%>
<%
            String moduleId = "24";
            Crypt crypt = new Crypt();
            try {
                crypt.setKey(crypt.Decifragem((String) session.getAttribute("userPhrase")));
                String desafio = crypt.Cifragem(moduleId + "@" + ferramentas.DataHora.formatarDataHora(ferramentas.DataHora.retornarDataHoraAtual(), ferramentas.DataHora.DATE_TIME_FORMAT));
                if (!(new Sessao(session).validarSessao(desafio))) {
                    response.sendRedirect("/" + request.getContextPath());
                }
            } catch (Exception e) {
                response.sendRedirect("/" + request.getContextPath());
            }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Gestor Operacional</title>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/style.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/table.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/anytime.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/jquery.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytime.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytimetz.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/table.js"></script>
    </head>
    <body>
        <table width="100%">
            <tr>
                <td>
                    <jsp:include page = "../menu.jsp" flush = "true"/>
                </td>
            </tr>
            <%
                        String processar = (String) request.getParameter("processar");
                        if ((processar != null) && (processar.equals("sim"))) {
                            String caminhoTemp = "c:/processar/";
                            String pasta = "c:/processar/";

                            String sal = String.valueOf(ferramentas.DataHora.formatarDataHora(ferramentas.DataHora.retornarDataHoraAtual(), "yyyy-MM-dd__HH_mm_ss__S___"));

                            if (FileUpload.isMultipartContent(request)) {
                                DiskFileUpload upload = new DiskFileUpload();
                                upload.setRepositoryPath(caminhoTemp);

                                try {
                                    List items = upload.parseRequest(request);
                                    Iterator iter = items.iterator();
                                    while (iter.hasNext()) {
                                        FileItem item = (FileItem) iter.next();
                                        if (!item.isFormField()) {
                                            String nome = item.getName().toString();
                                            String nomeArquivo = nome.substring(nome.lastIndexOf("\\") + 1);
                                            File arquivo = new File(pasta + nomeArquivo);
                                            item.write(arquivo);

                                            ReadExcel test = new ReadExcel();
                                            test.setInputFile(arquivo.getPath());
                                            test.processarColaboradores();
                                            test.processarAusencias();
                                            test.processarProducao();
                                            test.processarHorarios();
                                            test.processarServicos();

                                            BufferedReader in = new BufferedReader(new FileReader(arquivo.getPath() + "_processado.txt"));
                                            out.println("<tr><td  align=\"center\"><br><br><table border=\"1\" bgcolor=\"#FFFFFF\"><tr><td>");
                                            String resultado = "";
                                            while (in.ready()) {
                                                resultado += in.readLine() + "<br>";
                                            }
                                            out.println(resultado);
                                            EmailSender es = new EmailSender();
                                            es.setSubject("[GESTOR OPERACIONAL] Processamento de Planilha - " + arquivo.getName());
                                            es.setMessage("<H1>Resultado do Processamento</H1><br>" + resultado);
                                            String[] list = new String[1];
                                            list[0] = "op.ins.clr@brscan.com.br";
                                            es.setEmailList(list);
                                            try {
                                                es.postMail();
                                            } catch (Exception e) {
                                                System.out.println(e.getMessage());
                                            }
                                            in.close();
                                            out.println("</td></tr></table></tr></td>");
                                            ferramentas.Arquivos.moverPara(arquivo.getPath(), arquivo.getParent() + "/processado/" + sal + arquivo.getName() + "_processado");
                                            ferramentas.Arquivos.moverPara(arquivo.getPath() + "_processado.txt", arquivo.getParent() + "/processado/" + sal + arquivo.getName() + "_processado.txt");
                                        }
                                    }
                                } catch (FileUploadException e) {
                                    out.write("Não foi possível efetivar o upload do arquivo - segue a causa do erro" + "<br>" + e.getMessage());
                                    e.printStackTrace();
                                }
                            }
                        }
            %>

            <tr align="center">
                <td><br/><br/>
                    <form action="administracaoProcessamentoPlanilha.jsp?processar=sim" method="post" ENCTYPE="multipart/form-data">
                        <input type="file" name="arquivo" id="arquivo">
                        <input type="submit" value="Enviar">
                    </form>
                </td>
            </tr>
        </table>
    </body>
</html>